package gdonly.top.signin.utils;

import android.util.Log;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import gdonly.top.signin.entity.User;

/**
 * Created by zhuzhuxia on 2017/4/27.
 */
public class JsonUtil {
    /**
     * 把json格式的数据封装成student对象。
     * @param jsonData
     * @return
     */
    public static User getUserFromJson(String jsonData) throws JSONException {

        JSONObject object = new JSONObject(jsonData);
        int position = object.getInt("position");
        String userId = object.getString("id");
        String classId = object.getString("class_id");
        String mac = object.getString("mac");
        String ssid = object.getString("ssid");


        Log.e("TAG", ssid);
        User user = new User();
        user.setPosition(position);
        user.setId(userId);
        user.setClassId(classId);
        user.setMac(mac);
        user.setSsid(ssid);

        return user;
    }
    private static Object getFromJson(String jsonData, String key) throws JSONException {
        JSONObject object = new JSONObject(jsonData);
        return object.get(key);

    }
    public static int getHeadCountFromJson(String jsonData) throws JSONException {

        return (int) getFromJson(jsonData, "headcount");
    }
    public static boolean isSignEnable(String jsonData) throws JSONException {

        return (boolean) getFromJson(jsonData, "sign_enable");
    }
    public static int getActualSigned(String jsonData) throws JSONException {

        JSONObject object = new JSONObject(jsonData);
        return object.getInt("actual");
//        return (int) getFromJson(jsonData, "actual");
    }

    public static List<User> getUsersFromJson(String jsonArray) throws JSONException {
        JSONArray array = new JSONArray(jsonArray);
        List<User> users = new ArrayList<>();
        for(int i = 0; i < array.length(); i++){
            JSONObject object = array.getJSONObject(i);
            User user = new User();
            user.setId(object.getString("id"));
            user.setName(object.getString("name"));
            user.setSign(object.getBoolean("state"));
            users.add(user);


        }
        return users;
    }

    public static List<HashMap<String, Object>> getRecordsByUserId(String json) throws JSONException {
        JSONArray array = new JSONArray(json);
        Log.e("TAG",json);
        List<HashMap<String,Object>> data = new ArrayList<>();
        for(int i = 0; i < array.length(); i++){

            HashMap<String, Object> hashmap = new HashMap<>();
            JSONObject object = array.getJSONObject(i);


            hashmap.put("date", object.getString("date"));
            hashmap.put("state", object.getBoolean("state"));
            Log.e("TAG", object.getString("course_name"));
            hashmap.put("course_name", object.getString("course_name"));
            data.add(hashmap);
        }
        return data;
    }
    public static List<HashMap<String, Object>> getRecordsByCourse(String json) throws JSONException {
        JSONArray array = new JSONArray(json);
        Log.e("TAG",json);
        List<HashMap<String,Object>> data = new ArrayList<>();
        for(int i = 0; i < array.length(); i++){

            HashMap<String, Object> hashmap = new HashMap<>();
            JSONObject object = array.getJSONObject(i);


            hashmap.put("date", object.getString("date"));
            hashmap.put("rate", object.getString("rate"));

            hashmap.put("actual", object.getInt("actual"));
            hashmap.put("should", object.getInt("should"));

            data.add(hashmap);
        }
        return data;
    }

    public static List<HashMap<String, Object>> getStuFromCourse(String json) throws JSONException {
        JSONArray array = new JSONArray(json);
        Log.e("TAG",json);
        List<HashMap<String,Object>> data = new ArrayList<>();
        for(int i = 0; i < array.length(); i++){

            HashMap<String, Object> hashmap = new HashMap<>();
            JSONObject object = array.getJSONObject(i);


            hashmap.put("user_id", object.getString("user_id"));
            hashmap.put("user_name", object.getString("user_name"));

            hashmap.put("state",object.getBoolean("state"));
            data.add(hashmap);
        }
        return data;
    }
}
